function initValidation() {
    jQuery.validator.addMethod("unique",
        function (value, element) {
            var form = $(element).closest("form");
            var url = form.attr("action").split("!")[0] + "!checkUnique?element=" + $(element).attr("name");
            var id = form.find("input[name='id']");
            if (id.size() > 0) {
                url = url + "&id=" + id.val()
            }
            return $.validator.methods.remote.call(this, value, element, url)
        },
        "数据已存在");

    jQuery.validator.addMethod("timestamp",
        function (value, element) {
            if (value == "") {
                return this.optional(element)
            }
            var regex = /^(?:[0-9]{4})-(?:(?:0[1-9])|(?:1[0-2]))-(?:(?:[0-2][1-9])|(?:[1-3][0-1])) (?:(?:[0-2][0-3])|(?:[0-1][0-9])):[0-5][0-9]:[0-5][0-9]$/;
            if (!regex.test(value)) {
                return false
            }
            return true
        },
        "请输入合法的日期时间格式（如2011-08-15 13:40:00）");

    jQuery.validator.addMethod("yearMonth",
        function (value, element) {
            if (value == "") {
                return this.optional(element)
            }
            var regex = /^(?:[0-9]{4})(?:(?:0[1-9])|(?:1[0-2]))$/;
            if (!regex.test(value)) {
                return false
            }
            return true
        },
        "请输入合法的年月格式（如201201）");

    jQuery.validator.addMethod("startWith",
        function (value, element, param) {
            if (this.optional(element)) {
                return true
            }
            if (param.length > value.length) {
                return false
            }
            if (value.substr(0, param.length) == param) {
                return true
            } else {
                return false
            }
        },
        "请输入以{0}开头字符串");

    jQuery.validator.addMethod("dateLT",
        function (value, element, param) {
            if (value == "") {
                return this.optional(element)
            }
            var endDate = $(param).val();
            if (endDate == "") {
                return true
            }
            var startDate = eval("new Date(" + value.replace(/[\-\s:]/g, ",") + ")");
            endDate = eval("new Date(" + endDate.replace(/[\-\s:]/g, ",") + ")");
            if (startDate > endDate) {
                return false
            } else {
                return true
            }
        },
        "输入的日期数据必须小于结束日期");

    jQuery.validator.addMethod("dateGT",
        function (value, element, param) {
            if (value == "") {
                return this.optional(element)
            }

            var startDate = $(param).val();
            if (startDate == "") {
                return true
            }
            var endDate = eval("new Date(" + value.replace(/[\-\s:]/g, ",") + ")");
            startDate = eval("new Date(" + startDate.replace(/[\-\s:]/g, ",") + ")");
            if (startDate > endDate) {
                return false
            } else {
                return true
            }
        },
        "输入的日期数据必须大于开始日期");

    jQuery.validator.addMethod("tel",
        function (value, element, param) {
            if (value == "") {
                return this.optional(element)
            }
            var regex = /^(?:(?:0\d{2,3}[\- ]?[1-9]\d{6,7})|(?:[48]00[\- ]?[1-9]\d{6}))$/;
            if (!regex.test(value)) {
                return false
            }
            return true
        },
        "请填写有效的电话号码");

    jQuery.validator.addMethod("mobile",
        function (value, element, param) {
            if (value == "") {
                return this.optional(element)
            }
            var regex = /^1[3-9]\d{9}$/;
            if (!regex.test(value)) {
                return false
            }
            return true
        },
        "手机号格式不正确");

    jQuery.validator.addMethod("qq",
        function (value, element, param) {
            if (value == "") {
                return this.optional(element)
            }
            var regex = /^[1-9]\d{4,}$/;
            if (!regex.test(value)) {
                return false
            }
            return true
        },
        "QQ号格式不正确");

    jQuery.validator.addMethod("IDcard",
        function (value, element, param) {
            if (value == "") {
                return this.optional(element)
            }
            var regex = /^\d{6}(19|2\d)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)?$/;
            if (!regex.test(value)) {
                return false
            }
            return true
        },
        "请填写正确的身份证号码");

    jQuery.validator.addMethod("url",
        function (value, element, param) {
            if (value == "") {
                return this.optional(element)
            }
            var regex = /^(https?|s?ftp):\/\/\S+$/i;
            if (!regex.test(value)) {
                return false
            }
            return true
        },
        "请填写有效的网址");

    jQuery.validator.addMethod("chinese",
        function (value, element, param) {
            if (value == "") {
                return this.optional(element)
            }
            var regex = /^[\u0391-\uFFE5]+$/;
            if (!regex.test(value)) {
                return false
            }
            return true
        },
        "请输入中文");

    jQuery.validator.addMethod("username",
        function (value, element, param) {
            if (value == "") {
                return this.optional(element)
            }
            var regex = /^\w{3,12}$/;
            if (!regex.test(value)) {
                return false
            }
            return true
        },
        "请输入3-12位数字、字母、下划线");

    jQuery.validator.addMethod("password",
        function (value, element, param) {
            if (value == "") {
                return this.optional(element)
            }
            var regex = /^[\S]{6,16}$/;
            if (!regex.test(value)) {
                return false
            }
            return true
        }, "请填写6-16位字符，不能包含空格");

    jQuery.validator.addMethod("equalToByName",
        function (value, element, param) {
            var target = $(element).closest("form").find("input[name='" + param + "']");
            if (this.settings.onfocusout) {
                target.unbind(".validate-equalTo").bind("blur.validate-equalTo",
                    function () {
                        $(element).valid()
                    })
            }
            return value === target.val()
        }, "请输入前后相等数据");

    jQuery.validator.addMethod("zipCode",
        function (value, element) {
            var tel = /^[0-9]{6}$/;
            return this.optional(element) || (tel.test(value))
        }, "请输入有效的6位数字邮政编码");

}




+function ($) {
    var ExtValidate = function (f, e) {
        this.$element =  $(f);
        this.options = $.extend({}, ExtValidate.DEFAULTS, e);
        this.init()
    };
    ExtValidate.DEFAULTS={

    }
    ExtValidate.prototype.init = function () {
        var $form = this.$element,
            options = $form.data().options,
            options =  (options && typeof options === 'string') ?options.toObj():options,

            $els = $form.find(":input[type='text'], :input[type='password']," +
                " :input[type='radio'], :input[type='checkbox'], :input[type='file'],:input[type='hidden'], select , textarea");
        $els.each(function () {
            var $el = $(this);
            if ($el.attr("data-rule-required")) {
                var $formGroup = $el.closest(".bolt-inline").find(".bolt-form-label");
                ;
                if ($formGroup.length > 0 && $formGroup.find("span.required").length == 0) {
                    $formGroup.append('<span class="required">*</span>')
                }
            }
        });
        options = $.extend(options,{
            errorElement: "span",
            errorClass: "help-block",
            focusInvalid: true,
            ignore: ".note-editor *",
            errorPlacement: function (error, element) {
                element.closest(".bolt-input-inline").append(error)
            },
            highlight: function (element) { // hightlight error inputs
                // $(element).closest('.help-inline').removeClass('ok'); // display OK icon
                $(element).closest('.bolt-input-inline').removeClass('success').addClass('has-error'); // set error class to the control group
            },
            unhighlight: function (element) { // revert the change dony by hightlight
                $(element).closest('.bolt-input-inline').removeClass('has-error'); // set error class to the control group
            }
        })
        $form.validate(options);
    }
    function Plugin(e) {
        Bolt.assert($.fn.validate, "依赖组件 validate 未引入");
        return this.each(function () {
            var h = $(this);
            var g = h.data("extValidate");
            var f = typeof e == "object" && e;
            if (!g) {
                h.data("extValidate", (g = new ExtValidate(this, f)))
            }
            if (typeof e == "string") {
                g[e]()
            }
        })
    }
    $.fn.extValidate = Plugin
    $.fn.extValidate.Constructor = ExtValidate;

    Bolt.addComponent({
        name: "ExtValidate",
        plugin: Plugin,
        runPoint: Bolt.eventType.afterAjaxPageShow,
        expr: "form.form-validation",
        order: 800
    })
}(jQuery);
if ($.fn.validate) initValidation();